/*
 * name:小缘-hxxc
 * 更新时间:2016-3-31
 * 版本:v1.0.0
 * email:1440806569@qq.com
 */
(function(){
	//正确，感叹号，问号,笑脸,哭脸
	var icon = ["","&#xe601;","&#xe605;","&#xe604;","&#xe607;","&#xe606;"],
		//弹窗类型
		type = ["alert","confirm","prompt","msg"],
		//动画类型
		animateClass = [],
		//prompt类型 0:文本框 1:密码输入框 2:多行文本输入框
		prompt_type = ['<input type="text" class="input-box hxui-prompt-input"/>' , '<input type="input-box password" class="hxui-prompt-input"/>' , '<textarea class="input-box hxui-prompt-textarea"></textarea>'];
		i = 0;
	XuiModal.fn = XuiModal.prototype;
	//弹窗类
	function XuiModal(){
		//config
		this.config = {
			title : "提示",
			icon : false,
			itme : "",
			index : 0,
			zIndex : 999,
			prompt_type : false,
			modal_type : "",
			timer : "",
			mask : true,
			btn : ["确定" , "取消"]
		};
	};
	//alert
	XuiModal.fn.alert = function(t , json , confirm , cancel){
		//设置类型
		this.config.modal_type = type[0];
		//判断第二个参数是配置参数还是回调函数
		var f = "function" == typeof  json,
			t = "object" == typeof  t ? "" : t;
		f && (confirm = json);
		//继承
		var e = $.extend(this.config , {
			content : t,
			confirm : confirm,
			cancel : cancel
		} , f ? {} : json);
		//调用html生成
		this.vessel();
	};
	//confirm
	XuiModal.fn.confirm = function(t , json , confirm , cancel){
		//设置类型
		this.config.modal_type = type[1];
		//判断第二个参数是配置参数还是回调函数
		var f = "function" == typeof  json,
			t = "object" == typeof  t ? "" : t;;
		f && (confirm = json);
		//继承
		var e = $.extend(this.config , {
			content : t,
			confirm : confirm,
			cancel : cancel,
		} , f ? {} : json);
		//调用html生成
		this.vessel();
	};
	//prompt
	XuiModal.fn.prompt = function(json , confirm , cancel){
		this.config.modal_type = type[2];
		var f = "function" == typeof  json;
		f && (confirm = json);
		if(f){
			cancel = confirm;
			confirm = json;
		};
		//继承
		$.extend(this.config , {
			confirm : confirm,
			cancel : cancel
		}, f ? {} : json);
		this.vessel();
	};
	//msg
	XuiModal.fn.msg = function(t , json , cancel){
		this.config.modal_type = type[3];
		var f = "function" == typeof  json,
			t = "object" == typeof  t ? "" : t;
		f && (cancel = json);
		//继承
		var e = $.extend(this.config , {
			content : t,
			cancel : cancel
		} , f ? {} : json);
		this.vessel();
	};
	//生成html
	XuiModal.fn.vessel = function(){
		var th = this,
			config = th.config,
			mask,title,content,btn,moal;
			i++;
			//遮罩
			config.mask ? mask = '<div class="hxui-modal-mask '+ ("hxui-modal-"+i) +'" style=z-index:'+(config.zIndex)+'></div>' : "";
			//content
			content = '<div class="hxui-modal-content '+ (config.icon && (config.modal_type != type[2])  ? 'hxui-modal-IcoShow' : '') +'">'+ (config.modal_type == type[2] ? ((config.prompt_type && config.prompt_type < prompt_type.length) ? prompt_type[config.prompt_type] : prompt_type[0]) : (config.icon ? ("<i class='iconfont'>" + icon[config.icon] + "</i>") : ("")) + config.content) + '</div>';
			//btn
			if(config.modal_type != type[3]){
				//title
				title = '<div class="hxui-modal-title">'+ config.title +'</div>';
				//btn
				btn = '<div class="hxui-modal-btn"><a class="hxui-btn hxui-btn-choice">'+ config.btn[0] +'</a>' +(config.modal_type == type[0] ? "" : '<a class="hxui-btn hxui-modal-close">'+  config.btn[1]  +'</a>')+ '</div>';
			};
			//内容框
			moal = '<div class="hxui-modal '+("hxui-modal-"+i)+' hxui-modal-'+ config.modal_type +'" style=z-index:'+(config.zIndex +1)+'>'+ (config.modal_type == type[3] ? (btn = "") : ('<a class="hxui-modal-close hxui-title-close"><i class="iconfont">&#xe602;</i></a>' + title)) + content + btn +'</div>';
			config.mask && $("body").append(mask);
			$("body").append(moal);
			//判断是否自定义left和top
			th.set_margin($(".hxui-modal.hxui-modal-"+i));
			//判断是否自动隐藏
			if(config.modal_type == type[3]){
				config.time ? "" : config.time = 2000;
				th.auto_hiden(i);
			}else{
				config.time && th.auto_hiden(i);
			};
	};
	//set-margin
	XuiModal.fn.set_margin = function(element){
		var config = this.config,
			left = config.left ? config.left : false,
			top = config.top ? config.top : false;
		element.css({
			left :  left ? left  : "50%",
			top : top ? top : "50%",
			'margin-left' : left ? 0 : -(element.innerWidth() / 2),
			'margin-top' : top ? 0 : -(element.innerHeight() / 2)
		});
		//添加动画class
		element.addClass("hxui-modal-show");
		//绑定事件
		this.bind_event(i);
	};
	//绑定事件
	XuiModal.fn.bind_event = function(i){
		var th = this,
			val = "";
		//取消绑定
		$(".hxui-modal-"+i).find(".hxui-modal-close").on("click" , function(){
			th.config.cancel && th.config.cancel();
			th.remove(i);
		});
		//确定绑定
		$(".hxui-modal-"+i).find(".hxui-btn-choice").on("click" , function(){
			//val
			val = $(".hxui-modal-"+i).find(".input-box").val();
			if(th.config.modal_type == type[2]){
				if(th.config.required){
					val ? (th.config.confirm(val),th.remove(i)) : (XuiModal().msg("您还未输入任何内容哦!",{icon : 2 , time : 1000}));
				}else{
					th.config.confirm();
				};
			}else{
				th.config.confirm && th.config.confirm();
				th.remove(i);
			};
		});
	};
	//自动影藏
	XuiModal.fn.auto_hiden = function(i){
		var config = this.config,
			th = this;
		this.timer = setTimeout(function(){
			th.remove(i);
		} , config.time);
	};
	//移除
	XuiModal.fn.remove = function(i){
		clearInterval(this.timer);
		$(".hxui-modal-"+i).remove();
	};
	//版本
	XuiModal.fn.v = function(){
		console.log("作者:小缘-hxxc\n版本:V1.0.1\n更新时间:2016-3-30");
	};
	//暴露接口
	window.xuiModal = function(){
		return new XuiModal();
	};
})(window);